package com.fyzk.utils;

import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;

public class TextStyleUtil {

	
	/** 
	* 超链接 
	*/  
	public static SpannableString urlSpan(SpannableString spanString) {  
	    URLSpan span = new URLSpan("tel:0123456789");  
	    spanString.setSpan(span, 0, spanString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
	    return spanString;
	}  
	  
	  
	/** 
	* 文字背景颜色 
	*/  
	public static SpannableString backColorSpan(SpannableString spanString, int color) {  
	    BackgroundColorSpan span = new BackgroundColorSpan(color);  
	    spanString.setSpan(span, 0, spanString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
	    return spanString;
	}  
	  
	  
	/** 
	* 文字颜色 
	*/  
	public static SpannableString foreColorSpan(SpannableString spanString, int color) {  
	    ForegroundColorSpan span = new ForegroundColorSpan(color);  
	    spanString.setSpan(span, 0, spanString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
	    return spanString; 
	}
	/**
	 * 文字颜色
	 */
	public static SpannableString foreColorSpan(SpannableString spanString, int color, int startIndext, int endIndext) {
		if (endIndext > spanString.length()) {
			endIndext = spanString.length();
		}
		ForegroundColorSpan span = new ForegroundColorSpan(color);
		spanString.setSpan(span, startIndext, endIndext, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		return spanString;
	}
	  
	  
	/**
	 * 字体大小  
	 * @param spanString
	 * @param size	绝对值（dip）
	 * @return
	 */
	public static SpannableString fontSizeSpan(SpannableString spanString, int size) {  
	    AbsoluteSizeSpan span = new AbsoluteSizeSpan(size, true);  
	    spanString.setSpan(span, 0, spanString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
	    return spanString;  
	}  
	/**
	 * 字体大小 
	 * @param spanString
	 * @param size	默认字体大小的size倍 （例如： 1.5f）
	 * @return
	 */
	public static SpannableString fontSizeSpan(SpannableString spanString, float size) {  
	    RelativeSizeSpan span = new RelativeSizeSpan(size);
	    spanString.setSpan(span, 0, spanString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
	    return spanString;  
	}  
	  
	  
	/** 
	* 粗体，斜体 
	*/  
	public static SpannableString styleSpan(SpannableString spanString, int typeface) {  
	    StyleSpan span = new StyleSpan(typeface);  
	    spanString.setSpan(span, 0, spanString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
	    return spanString;
	}  
	  
	  
	/** 
	* 删除线 
	*/  
	public static SpannableString strikeSpan(SpannableString spanString) {  
	    StrikethroughSpan span = new StrikethroughSpan();  
	    spanString.setSpan(span, 0, spanString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
	    return spanString;  
	}  
	  
	/** 
	* 下划线 
	*/  
	public static SpannableString underLineSpan(SpannableString spanString) {  
	    UnderlineSpan span = new UnderlineSpan();  
	    spanString.setSpan(span, 0, spanString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
	    return spanString; 
	}  
	  
	  
	  
	/** 
	* 图片 
	*/  
	public static SpannableString imageSpan(SpannableString spanString, Drawable d) {  
	    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());  
	    ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);  
	    spanString.setSpan(span, 0, spanString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
	    return spanString;  
	}  
}
